--编写者：萌新小猫
--功能介绍：风滚草额外掉落
--使用方法：直接粘贴到模组环境 "abc"换成角色标签 addlucky方法换成额外执行的内容 目前是额外掉落草、树枝，并再随机加一个

local luck_loot = {
    {chance = 1,    item = "petals"},
    {chance = 1,    item = "foliage"},
    {chance = 1,    item = "silk"},
    {chance = 1,    item = "rope"},
    {chance = 2,    item = "seeds"},
    {chance = 0.01, item = "purplegem"},
    {chance = 0.04, item = "bluegem"},
    {chance = 0.02, item = "redgem"},
    {chance = 0.02, item = "orangegem"},
    {chance = 0.01, item = "yellowgem"},
    {chance = 0.02, item = "greengem"},
    {chance = 0.5,  item = "trinket_6"},
    {chance = 0.5,  item = "trinket_4"},
    {chance = 1,    item = "cutreeds"},
    {chance = 0.33, item = "feather_crow"},
    {chance = 0.33, item = "feather_robin"},
    {chance = 0.33, item = "feather_robin_winter"},
    {chance = 0.33, item = "feather_canary"},
    {chance = 1,    item = "trinket_3"},
    {chance = 1,    item = "beefalowool"},
    {chance = 0.1,  item = "rabbit"},
    {chance = 0.1,  item = "mole"},
    {chance = 1,    item = "butterflywings"},
    {chance = .02,  item = "beardhair"},
    {chance = 1,    item = "berries"},
    {chance = 1,    item = "blueprint"},
    {chance = 1,    item = "petals_evil"},
    {chance = 1,    item = "trinket_8"},
    {chance = 1,    item = "houndstooth"},
    {chance = 1,    item = "stinger"},
    {chance = 1,    item = "gears"},
    {chance = 0.1,  item = "boneshard"},
    {chance = 0.25, item = "cookingrecipecard"},
    {chance = 0.25,  item = "scrapbook_page"},
}

local CHESS_LOOT =
{
    -- "chesspiece_pawn_sketch",
    -- "chesspiece_muse_sketch",
    -- "chesspiece_formal_sketch",
    "trinket_15", --bishop
    "trinket_16", --bishop
    "trinket_28", --rook
    "trinket_29", --rook
    "trinket_30", --knight
    "trinket_31", --knight
}

local totalchance = 0
for m, n in ipairs(luck_loot) do
    totalchance = totalchance + n.chance
end

local function GetEItem()
    local chance = math.random()*totalchance
    for m, n in ipairs(luck_loot) do
        chance = chance - n.chance
        if chance <= 0 then
            return n.item
        end
    end
    return "charcoal"
end

local function addlucky(inst)
    local x, y, z = inst.Transform:GetWorldPosition()
    local loots = {"cutgrass", "twigs"}

    local chessunlocks = TheWorld.components.chessunlocks
    if chessunlocks ~= nil and math.random() <= .2 then
        TheNet:Say(1)
        local chessitem = CHESS_LOOT[math.random(#CHESS_LOOT)]
        if not chessunlocks:IsLocked(chessitem)  then
            table.insert(loots, chessitem)
        end
    end
    table.insert(loots, GetEItem())

    local item
    for i, v in ipairs(loots) do
        item = SpawnPrefab(v)
        if item ~= nil then
            item.Transform:SetPosition(x, y, z)

            if item.components.inventoryitem ~= nil and item.components.inventoryitem.ondropfn ~= nil then
                item.components.inventoryitem.ondropfn(item)
            end
        end
    end
end

AddPrefabPostInit("tumbleweed", function(inst)
    if not TheWorld.ismastersim then
        return
    end

    if inst.components.pickable then
        local _onpickedfn = inst.components.pickable.onpickedfn
        inst.components.pickable.onpickedfn = function(inst, picker, ...)
            if picker and picker:HasTag("abc") then
                addlucky(inst)
            end
            return _onpickedfn(inst, picker, ...)
        end
    end
end)